package online.sh.cn.fy.users.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;
import online.sh.cn.fy.common.utils.WxUtils;

/**
 * 更新user信息 
 */
@WebServlet("/updateuser")
public class UpdateUserServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		WxUtils wx=new WxUtils();
		String name=request.getParameter("name");
		String mobile=request.getParameter("mobile");
		String school=request.getParameter("school");
		String building=request.getParameter("building");
		String schoolgrade=request.getParameter("schoolgrade");
		String schoolmajor=request.getParameter("schoolmajor");
		String belong=wx.getMoblieinfo(mobile).getString("province");
		String operator=wx.getMoblieinfo(mobile).getString("catName");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		try {
			openid=DES3Util.decrypt(openid);
		} catch (Exception e) {
			log(e.getMessage());
		}
		System.out.println("更新用户openid:"+openid);
		JSONObject json=new JSONObject();
		TokenUtils to=new TokenUtils();
		if(null!=openid && ""!=openid){
			String checkmobile=to.getMobileBand(mobile);
			if (checkmobile.equals(mobile)) {
				to.deleteMobileBandByOpenid(mobile,openid);
				to.UpdateFyUser(openid, mobile, belong, operator, name, school, building, schoolgrade, schoolmajor);
			}else{
				to.UpdateFyUser(openid, mobile, belong, operator, name, school, building, schoolgrade, schoolmajor);
			}
			json.put("result", "0"); //验证成功 入库 
			ResponseUtils.renderJson(response, json.toString());
		}else{
			json.put("result", "3");   //openid未获取
			ResponseUtils.renderJson(response, json.toString());
		}
	}

}
